/**
 * 
 */
package org.javaongems.client.forms;

import org.javaongems.client.Gem;
import org.javaongems.client.stdctrls.Image;
import org.javaongems.client.stdctrls.ProgressBar;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SourcesMouseEvents;

public class Frame extends FlowPanel {
	final static public String FORM_RESIZE_ICON = "gems-Form-resize.gif";
	final static public String FORM_MIN_ICON = "gems-Form-min.gif";
	final static public String FORM_MAX_ICON = "gems-Form-max.gif";
	final static public String FORM_CLOSE_ICON = "gems-Form-close.gif";
	final static public String FORM_LOADING_ICON = "gems-loading.gif";

	protected SourceMouseEventsFlowPanel captionArea = new SourceMouseEventsFlowPanel();
	protected ScrollPanel contentArea = new ScrollPanel();
	protected FlowPanel statusArea = new FlowPanel();
	
	protected FlowPanel loadingContainer = new FlowPanel();
	protected FlowPanel borderButtons = new FlowPanel();
	protected FlowPanel statusProgressContainer = new FlowPanel();

	protected String imagesCtx = Gem.stone.getApplImageCtx();
	protected HTML caption = new HTML();
	protected HTML status = new HTML();
	protected Image closeButton = new Image(imagesCtx + FORM_CLOSE_ICON);
	protected Image maxButton = new Image(imagesCtx + FORM_MAX_ICON);
	protected Image minButton = new Image(imagesCtx + FORM_MIN_ICON);
	protected Image resizeHandle = new Image(imagesCtx + FORM_RESIZE_ICON);
	protected ProgressBar progressBar = new ProgressBar();
	protected com.google.gwt.user.client.ui.Image formIcon;
	protected Image loadIcon = new Image(imagesCtx + FORM_LOADING_ICON);
	
	public Frame() {
		initialiseLayout();
		initialiseCssClass();
	}
	
	protected void initialiseCssClass() {
		this.setStyleName("gems-Form");
		captionArea.setStyleName("gems-Caption");
		contentArea.setStyleName("gems-Content");
		statusArea.setStyleName("gems-Status");
	}
	
	protected void initialiseLayout() {
		add(captionArea);
		add(contentArea);
		add(statusArea);
		
		captionArea.add(loadingContainer);
		captionArea.add(caption);
		captionArea.add(borderButtons);
		borderButtons.add(minButton);
		borderButtons.add(maxButton);
		borderButtons.add(closeButton);
		
		statusArea.add(statusProgressContainer);
		statusProgressContainer.add(status);
		statusProgressContainer.add(progressBar);
		
		add(resizeHandle);
	}
	
	// exports

	public void setFormIcon(com.google.gwt.user.client.ui.Image img) {
		formIcon = img;
	}
	
	public void showLoadingIcon() {
		
	}
	
	public void adjustFramesContentSize() {
		int height = DOM.getIntStyleAttribute(this.getElement(), "height");
		int captionHeight = DOM.getIntStyleAttribute(captionArea.getElement(), "height");
		int statusHeight = DOM.getIntStyleAttribute(statusArea.getElement(), "height");
		int contentHeight = height - captionHeight - statusHeight;
		DOM.setIntStyleAttribute(contentArea.getElement(), "height", contentHeight);
	}
	
	public SourcesMouseEvents getMinWidgetSourcesMouseEvents() {
		return minButton;
	}

	public SourcesMouseEvents getMaxWidgetSourcesMouseEvents() {
		return maxButton;
	}

	public SourcesMouseEvents getCloseWidgetSourcesMouseEvents() {
		return closeButton;
	}

	public SourcesMouseEvents getResizeWidgetSourcesMouseEvents() {
		return resizeHandle;
	}
	
	public SourcesMouseEvents getMoveWidgetSourcesMouseEvents() {
		return captionArea;
	}
	
	static protected class SourceMouseEventsFlowPanel extends FlowPanel implements SourcesMouseEvents {
		protected MouseListenerCollection mouseListeners;

		public SourceMouseEventsFlowPanel() {
			super();
			sinkEvents(Event.MOUSEEVENTS);
		}

		public void addMouseListener(MouseListener listener) {
			if (mouseListeners == null)
				mouseListeners = new MouseListenerCollection();
			mouseListeners.add(listener);
		}

		public void removeMouseListener(MouseListener listener) {
			if (mouseListeners != null)
				mouseListeners.remove(listener);
		}

		public void onBrowserEvent(Event event) {
			switch (DOM.eventGetType(event)) {
			case Event.ONMOUSEDOWN:
			case Event.ONMOUSEUP:
			case Event.ONMOUSEMOVE:
			case Event.ONMOUSEOVER:
			case Event.ONMOUSEOUT:
				if (mouseListeners != null)
					mouseListeners.fireMouseEvent(this, event);
				break;
			}
		}
	}
}